
var app = getApp();
const $ = require('../../utils/utils');
const config = require('../../config');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        footerConfig: {
            loadingText: "正在努力的加载数据...",
        },
        imgurl: '',
        book_count: 0,
        rank_percentage: 0,
        user_name: '',
        rank: 0,
        rankList: [],
        shareImg: '',
        rankTitle: '',
        isShow: true
    },

/**
 * 生命周期函数--监听页面显示
 */
    onShow: function () {
        if (!wx.getStorageSync("isLogin")) {
            $.goto(config.page.login + "?path='weekRank'")
            return
        }
        let pageLength = getCurrentPages().length
        if (pageLength > 1) {
            this.setData({
                isShow: false
            })

        }
        this.queryPersonInWeekRank()
    },

    /***
     * methods
     * **/
    gotoIndex: function () {
        $.goto(config.page.index, true)
    },


    
    queryPersonInWeekRank: function () {
        let data = {
            userid: wx.getStorageSync('userid'),
            usertoken: wx.getStorageSync('usertoken')
        }
        let url = $.getUrl(config.queryPersonInWeekRank, data)
        $.get(url).then(data => {
            this.setData({
                imgurl: data.detail.imgurl,
                book_count: data.detail.book_count,
                rank_percentage: data.detail.rank_percentage,
                user_name: data.detail.user_name,
                rank: data.detail.rank,
                shareImg: data.detail.shareUrl,
                rankTitle: data.detail.rankTitle
            })
            this.queryWeekRankList()
        })
    },

    queryWeekRankList: function () {
        let data = {
            userid: wx.getStorageSync('userid'),
            usertoken: wx.getStorageSync('usertoken')
        }
        let url = $.getUrl(config.queryWeekRankList, data)
        $.get(url).then(data => {
            this.setData({
                rankList: data.items
            })
        })
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        console.log("buttom", res);
        if (res.from === 'button') {
            // 来自页面内转发按钮
            console.log(res.target);
        }
        return {
            title: this.data.rankTitle,
            imageUrl: this.data.shareImg,
            path: config.page.weekRankList,
            success: function (res) {
                console.log("分享成功", wx.getStorageSync("hdCardInitiatorId"));
            },
            fail: function (res) {
                // 转发失败
                console.log("fail");
            }
        }
    }
})